var app = getApp();
var socket = null;
var socketOpen = false;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderlist: [],
    shopid:"",
    ison:1,//营业状态,
    shop_type:1,//1：洗车   3：加油,
    now_page:1,
    total_page:0,
    isShowAlert:false,
    order_id:"",
    number:"",
    order_index:0,
    user_role:0,
    msg_count:0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shop_type:wx.getStorageSync('shop_type'),
      user_role:wx.getStorageSync('role')
    })
    
    this.getOnlineStatus();
  
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMsgNUm();
    this.setData({
      orderlist:[]
    })
    this.getorder();
  },
   // 隐藏员工编辑弹框
   hideAlert(){
    this.setData({
      isShowAlert:false
    })
  },
  // 确认订单
  confirmOrder(e){
    let url = 'api/shopr/confirmCarOrder';
    let number = e.detail.value ?  e.detail.value.number :''; 
    let index = e.target.dataset.index;
    let list = this.data.orderlist;
    if(typeof index == 'number'){
      this.setData({
        order_index:e.target.dataset.index,
      })
    }
    let i = this.data.order_index;
    app.post({
      url: url,
      data:{
        order_id:list[i].id,
        user_id:wx.getStorageSync('userId'),
        number:number
      },
      success:(e)=>{
        let res = e.data.data;
        if(res.type == 1){
          console.log(res)
          this.setData({
            isShowAlert:true,
            number:res.number
          })
          return
        }
        wx.showToast({
          title: '确认成功',
          icon:'none'
        })
        this.hideAlert();
   
        list.splice(i,1);
        this.setData({
          orderlist:list
        })
      }
    })
  },
  // 改变营业状态
  changestatus(e){
    let ison = e.currentTarget.dataset.type;
   
    if(ison != this.data.ison){
      var that = this;
      var shopid = wx.getStorageSync('shop_id');
      console.log(ison)
      app.post({
        url: 'api/shop/changeIson',
        data: {
          shop_id: shopid,
          ison: ison
        },
        success: function (res) {
          wx.showToast({
            title: res.data.msg,
            icon:'none',
            duration:2000
          })
          if(res.data.code == 1){
            that.setData({
              ison:ison
            })
          }
        }
  
      });
    }
   
  },
  // 获取营业状态
  getOnlineStatus(){
      app.post({
        url:"api/shopr/getOnStatus",
        data:{
          shop_id:wx.getStorageSync("shop_id")
        },
        success:res=>{
          console.log(res)
          this.setData({
            ison:res.data.data.ison
          })
        }
      })
  },
  // 获取订单信息
  getorder: function (now_page = 1) {
    var that = this;
    var shopid = wx.getStorageSync("shop_id");
    let url ='api/shopr/carOrder'
    this.setData({
      shopid:shopid,
    })
    var param = {
      url: url,
      data: {
        shop_id: shopid,
        type:1,
        now_page:now_page
      },
      success:(e)=> {
        let res = e.data.data;
        this.setData({
          orderlist:this.data.orderlist.concat(res.list),
          total_page:res.total_page
        })

      }
    };
    app.post(param);
  },
onReachBottom(){
  let now_page = this.data.now_page;
  console.log(now_page,this.data.total_page)
  if(now_page < this.data.total_page){
    now_page++;
    this.setData({
      now_page:now_page
    })
    this.getorder(now_page)
  }
},
getMsgNUm: function () {
    let url = 'api/shopr/getMsgNum';
    var data = {
      url: url,
      data: {
        shop_id: wx.getStorageSync('shop_id')
      },
      success: (e) => {
        this.setData({
          msg_count: e.data.data.count
        });
      }
    };
    app.post(data);
}

})